package com.insurance.controller;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.pojo.CouponsInfo;
import com.insurance.pojo.User;
import com.insurance.pojo.User_coupon;
import com.insurance.service.CouponService;
import com.insurance.utils.ConversionDateUtils;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.plugin.com.Utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User1: 李辽
 * Date: 2020/10/27 11:23
 * Description:优惠卷相关操作
 * Version: V1.0
 */
@Api(value = "优惠卷",tags = "优惠卷信息API")
@RequestMapping(value = "/coupon")
@Controller
public class CouponController {

    @Autowired
    private CouponService couponService;

    @RequestMapping(value = "/queryCoupon.do",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有或者多条件查询优惠卷信息",notes = "查询所有或者多条件查询优惠卷信息接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="优惠卷信息查询成功"),
            @ApiResponse(code=500,message="后台服务错误"),
    })

    @ApiImplicitParams({
            @ApiImplicitParam(name = "disbursement_id",value = "发放方式id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "usearea_id",value = "使用范围id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "start_time",value = "开始时间",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "end_time",value = "结束时间",dataType = "String",paramType = "query")
    })
    @ResponseBody
    //查询所有或者多条件查询优惠卷信息
    public ResultData queryCoupon(String start_time,String end_time,Integer disbursement_id,Integer usearea_id ){
        ResultData resultData = new ResultData();
        List<Map> mapList = couponService.queryCoupon(start_time,end_time,disbursement_id,usearea_id);
        if (mapList != null){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("优惠卷信息查询成功");
            resultData.setData(mapList);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
        }
        return resultData;
    }



    @RequestMapping(value = "/editCoupon.do",method = RequestMethod.POST)
    @ApiOperation(value = "更改优惠卷信息",notes = "更改优惠卷信息接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="更改成功"),
            @ApiResponse(code=500,message="后台服务错误"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId",value = "优惠卷id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "couponName",value = "优惠券名称",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "disbursementId",value = "发放方式id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "useareaId",value = "使用范围id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "startTime",value = "开始时间",required = true,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "endTime",value = "结束时间",required = true,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "price",value = "优惠价格",required = true,dataType = "double",paramType = "query")
    })
    @ResponseBody
    //更改优惠卷信息
    public ResultData editCoupon(@RequestBody CouponsInfo couponsInfo){
        //将前端获取的String类型的时间格式转换为date格式
        couponsInfo.setEnd_time(ConversionDateUtils.conversionDateUtils(couponsInfo.getEndTime()));
        couponsInfo.setStart_time(ConversionDateUtils.conversionDateUtils(couponsInfo.getStartTime()));

        System.out.println("更改数据为："+couponsInfo);

        ResultData resultData = new ResultData();
        int result = couponService.editCoupon(couponsInfo);
        if(result > 0 ){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("更改成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
        }
        return resultData;
    }



    @RequestMapping(value = "/sendCoupon.do",method = RequestMethod.POST)
    @ApiOperation(value = "发送优惠卷信息",notes = "发送优惠卷信息接口",httpMethod = "post",response = ResultData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id",value = "用户id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "user_phone",value = "用户手机号",required = true,dataType = "int",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="优惠卷发送成功"),
            @ApiResponse(code=500,message="空指针异常、表示无此电话号码"),
    })
    @ResponseBody
    //发送优惠卷信息接口
    public ResultData sendCoupon(@RequestBody User_coupon user_coupon ){
        System.out.println("前端输入优惠卷信息为；"+user_coupon+"   "+user_coupon.getUser_phone() );
        ResultData resultData = new ResultData();
        int result = couponService.sendCoupon(user_coupon);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("优惠卷发送成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("空指针异常");
        }
        return resultData;
    }



    @RequestMapping(value = "/judgePhone.do",method = RequestMethod.POST)
    @ApiOperation(value = "判断用户手机是否存在",notes = "判断用户手机是否存在信息接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="该手机号存在"),
            @ApiResponse(code=500,message="该手机号不存在"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_phone",value = "用户手机号",required = true,dataType = "String",paramType = "query"),
    })
    @ResponseBody
    //判断用户手机是否存在
    public ResultData judgePhone(@Param("user_phone") String user_phone){
        ResultData resultData = new ResultData();
        System.out.println("前端请求的电话号码为："+user_phone);
        int result = couponService.judgePhone(user_phone);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("该手机号存在");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("该手机号不存在");
        }
        return resultData;
    }





    @RequestMapping(value = "/addCoupon.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "添加优惠卷信息",notes = "添加优惠卷信息接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=500,message="后台服务错误"),
    })
    @ResponseBody
    //添加优惠卷信息
    public ResultData addCoupon(@RequestBody CouponsInfo couponsInfo){
        System.out.println("从前端获取到增加优惠卷信息为："+couponsInfo);
        System.out.println("字符串转日期，开始时间。"+ConversionDateUtils.conversionDateUtils(couponsInfo.getStartTime()));
        ResultData resultData = new ResultData();
        Date start_time = ConversionDateUtils.conversionDateUtils(couponsInfo.getStartTime());
        couponsInfo.setStart_time(start_time);
        Date end_time = ConversionDateUtils.conversionDateUtils(couponsInfo.getEndTime());
        couponsInfo.setEnd_time(end_time);

        int result = couponService.addCoupon(couponsInfo);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("优惠卷添加成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
        }
        return resultData;
    }



    @RequestMapping(value = "/qeuryUseArea.do",method = RequestMethod.GET)
    @ApiOperation(value = "查询优惠卷使用范围，返回前端，渲染下拉框",notes = "查询优惠卷使用范围，返回前端，渲染下拉框",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询优惠卷使用范围成功"),
            @ApiResponse(code=500,message="后台服务错误"),
    })
    @ResponseBody
    //查询优惠卷使用范围，返回前端，渲染下拉框
    public ResultData qeuryUseArea(){
        ResultData resultData = new ResultData();
        List<Map> mapList = couponService.qeuryUseArea();
        if (mapList != null){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询优惠卷使用范围成功");
            resultData.setData(mapList);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
        }

        return resultData;
    }



    @RequestMapping(value = "/qeuryDisbursement.do",method = RequestMethod.GET)
    @ApiOperation(value = "查询优惠卷发放方式，返回前端，渲染下拉框",notes = "查询优惠卷发放方式，返回前端，渲染下拉框",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询优惠卷发放方式成功"),
            @ApiResponse(code=500,message="后台服务错误"),
    })
    @ResponseBody
    //查询优惠卷发放方式，返回前端，渲染下拉框
    public ResultData qeuryDisbursement(){
        ResultData resultData = new ResultData();
        List<Map> mapList = couponService.qeuryDisbursement();
        if (mapList != null){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询优惠卷发放方式成功");
            resultData.setData(mapList);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务错误");
        }
        return resultData;
    }



}
